package com.alibaba.mq.consumer.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author louxiujun
 * @Date 2020/6/27 21:06
 **/
@Component
public class MyAckReceiver implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //因为传递消息的时候用的map传递,所以将Map从Message内取出需要做些处理
            String msg = message.toString();
            //可以点进Message里面看源码,单引号直接的数据就是我们的map消息数据
            String[] msgArray = msg.split("'");
            String messageBody = msgArray[1].trim();

            Map<String, String> msgMap = mapStringToMap(messageBody);
            String messageId = msgMap.get("messageId");
            String messageData = msgMap.get("messageData");
            String createTime = msgMap.get("createTime");

            String queueName = message.getMessageProperties().getConsumerQueue();
            switch (queueName) {
                case "TestDirectQueue":
                    // handler for queue TestDirectQueue
                    System.out.println("消费的消息来自的队列名为：" + message.getMessageProperties().getConsumerQueue());
                    System.out.println("消息成功消费到  messageId:" + messageId + "  messageData:" + messageData + "  createTime:" + createTime);
                    System.out.println("执行TestDirectQueue中的消息的业务处理流程......");
                    break;
                case "TestDirectQueue2":
                    // handler for queue TestDirectQueue2
                    System.out.println("消费的消息来自的队列名为：" + message.getMessageProperties().getConsumerQueue());
                    System.out.println("消息成功消费到  messageId:" + messageId + "  messageData:" + messageData + "  createTime:" + createTime);
                    System.out.println("执行fanout.A中的消息的业务处理流程......");
                    break;
                case "TestDirectQueue3":
                    // handler for queue TestDirectQueue3
                    break;
                default:
                    break;
            }

            channel.basicAck(deliveryTag, true);

//			channel.basicReject(deliveryTag, true);//为true会重新放回队列
        } catch (Exception e) {
            channel.basicReject(deliveryTag, true);
            e.printStackTrace();
        }
    }

    //{key=value,key=value,key=value} 格式转换成map
    private Map<String, String> mapStringToMap(String str) {
        str = str.substring(1, str.length() - 1);
        String[] strs = str.split(",");
        Map<String, String> map = new HashMap<String, String>();
        for (String string : strs) {
            String key = string.split("=")[0].trim();
            String value = string.split("=")[1];
            map.put(key, value);
        }
        return map;
    }

}
